package com.sk.cardeal.act.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.CustomAdapter;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.Custom;
import com.sk.cardeal.entity.CustomDetails;
import com.sk.cardeal.entity.CustomList;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/12/19.
 */

public class SearchCustomActivity extends BaseActivity implements HandleDataCallBack, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.ed_search)
    EditText edSearch;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.car_num)
    TextView carNum;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    private LoadingDialog loadingDialog;
    private List<Custom> allDataList = new ArrayList<>();
    private boolean isLoadMore = false;
    private CustomAdapter adapter;
    private int page = 0;
    private String searchString = "";

    @Override
    protected int getContentView() {
        return R.layout.activity_search_custom;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadingDialog = MyUtils.getLoadingDialog(mContext, false);

        edSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    if (edSearch.getText().toString().length() > 0) {
                        page = 0;
                        searchString = edSearch.getText().toString();
                        loadingDialog.show();
                        AppApi.getCustomList(-1, page, searchString, SearchCustomActivity.this, 0, SearchCustomActivity.this);
                    } else {
                        Toast.makeText(mContext, "搜索条件不能为空!", Toast.LENGTH_SHORT).show();
                    }
                }
                return false;
            }
        });

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new CustomAdapter(allDataList);
        dataRyc.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);
    }

    @OnClick({R.id.back, R.id.cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.cancel:
                finish();
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        CustomList customList = JSON.parseObject(result, CustomList.class);
        List<Custom> dataBeanList = customList.getResultList();

        if (page == 0) {
            allDataList.clear();
        }

        allDataList.addAll(dataBeanList);
        if (allDataList.size() < 10) {
            adapter.setEnableLoadMore(false);
        } else {
            adapter.setEnableLoadMore(true);
        }
        if (isLoadMore) {
            adapter.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                adapter.loadMoreEnd();
            }
            isLoadMore = false;
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, CustomDetailsActivity.class);
        intent.putExtra("id", allDataList.get(position).getId());
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        isLoadMore = true;
        AppApi.getCustomList(-1, page, searchString, SearchCustomActivity.this, 0, SearchCustomActivity.this);
    }
}
